package com.spring.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
	public static int getIntervalDays(Date startDt, Date endDt){
		if(startDt.after(endDt)){
            Date cal=startDt;
            startDt=endDt;
            endDt=cal;
        }        
        long sl=startDt.getTime();
        long el=startDt.getTime();       
        long ei=el-sl;           
        return (int)(ei/(1000*60*60*24));
	}
	
	public static Timestamp parseStr2Time(String str){
		return Timestamp.valueOf("1970-01-01 "+str+":00");
	}
	
	public static Date addDays(Date date, int days){
		Calendar cln = Calendar.getInstance();
		cln.setTime(date);
		cln.add(Calendar.DATE, days);
		return cln.getTime();
	}
	
	public static int getWeekDay(Date date){		
		Calendar cln = Calendar.getInstance();
		cln.setTime(date);
		int day = cln.get(Calendar.DAY_OF_WEEK)-1;
	    return (day<1)?7:day;
	}
	
	public static Date parseStr2Date(String str){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return dateFormat.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Date parseStr2Date(String str,String format){
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		try {
			return dateFormat.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String parseDate2Str(String pattern,Date date)
    {
        if (null == pattern || "".equals(pattern))
        {
            pattern = "yyyy-MM-dd";
        }
        if(date==null)
        	return "";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String dt = sdf.format(date);
        return dt;
    } 
	
	
	public static String getNowDate(){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");		
	    return dateFormat.format(Calendar.getInstance().getTime());		
	}
	
	public static String getDateTime(String pattern)
    {
        if (null == pattern || "".equals(pattern))
        {
            pattern = "yyyyMMdd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String dt = sdf.format(new Date());
        return dt;
    } 
	
	public static List<String> getMonthDay(int year,int month) {
		List<String> list = new ArrayList<String>();
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month-1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		int end = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		for(int i=1;i<=end;i++){
			Date date=c.getTime();
			list.add(DateUtil.parseDate2Str("yyyy-MM-dd",date));
			c.add(Calendar.DAY_OF_MONTH, 1);
		}
		return list;
	}
	/**
	 * 获取某年某月所有天，分闰年和平年
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getDays(int year, int month) {
		String strDays = "";
		int dayCount = dayCount(year, month);
		for (int i = 1; i <= dayCount; i++) {
			strDays += ","  + i + "";
		}
		return strDays.substring(1);
	}

	/**
	 * 获取某年某月的总天数
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public  static int dayCount(int year, int month) {
		int count = 0;
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			count = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			count = 30;
			break;
		case 2:
			if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
				count = 29;
			else
				count = 28;
		}
		return count;
	}
	/**
	 * 获取最近四年的年份
	 * @param 
	 * @return String
	 */
	public static String get4Years(){
		int thisYear = Calendar.getInstance().get(Calendar.YEAR);
		return (thisYear-3)+","+(thisYear-2)+","+(thisYear-1)+","+thisYear;
	}
	/**
	 * 获取一年所有月份
	 * 
	 * @return
	 */
	public static String getMonths() {
		String strMonths = "";
		for (int i = 1; i <= 12; i++) {
			strMonths += "," + i;
		}
		return strMonths.substring(1);
	}
}
